import java.util.Comparator;
import java.util.PriorityQueue;
class IntCmp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}
public class LeetCode {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(new IntCmp());
        for (int i = 0; i < stones.length; i++) {
            queue.offer(stones[i]);
        }
        while(queue.size()>1){
            int a = queue.poll();
            int b = queue.poll();
            if(a>b){
                queue.offer(a-b);
            }
        }
        return queue.isEmpty()?0: queue.peek();
    }
}
